﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _019_委托详解_3_一般使用
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ProductFactory productFactory = new ProductFactory();
            WrapFactory wrapFactory = new WrapFactory();
            Logger logger = new Logger();
            Action<Product> log = new Action<Product>(logger.CteateLog);
            //委托 productFactory中的MakePizza  返回值为Product
            Func<Product> func1 = new Func<Product>(productFactory.MakePizza);
            Func<Product> func2 = new Func<Product>(productFactory.MakeToCar);

            Box box1 = wrapFactory.WrapProduct(func1, log);
            Box box2 = wrapFactory.WrapProduct(func2, log);
            Console.WriteLine(box1.Product.Name);
            Console.WriteLine(box2.Product.Name);
        }
    }

    interface IProductFactory
    {
        Product Make();
    }

    class PizzaFactory : IProductFactory
    {

        public Product Make()
        {
            Product product = new Product();
            product.Name = "Pizza";
            return product;
        }
    }

    class ToyCarFactory { }

    //程序日志
    class Logger
    {
        public void CteateLog(Product product)
        {
            Console.WriteLine("Product '{0}' created at {1} .Price is {2},", product.Name, DateTime.UtcNow, product.Price);
        }
    }


    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }

    class Box
    {
        public Product Product { get; set; }
    }

    class WrapFactory
    {
        public Box WrapProduct(Func<Product> getProduct, Action<Product> CreateLogCallback)
        {
            Box box = new Box();
            Product product = getProduct();
            if (product.Price > 50)
            {
                CreateLogCallback(product);
            }
            box.Product = product;
            return box;
        }

    }
    class ProductFactory
    {
        public Product MakePizza()
        {
            Product product = new Product();
            product.Name = "Pizza";
            product.Price = 12;
            return product;
        }

        public Product MakeToCar()
        {
            Product product = new Product();
            product.Name = "Toy Car";
            product.Price = 100;
            return product;
        }
    }
}
